package com.gitee.wsl.common.draw.area

import android.graphics.*
import com.gitee.wsl.common.ui.draw.type.GridDirection
import com.gitee.wsl.mathematics.geometry.shape.Rectangle

/**
 * @ClassName Area
 * @Description 主图表区类，用于定制其属性
 * @author XiongChuanLiang<br/>(xcl_168@aliyun.com)
 *
 */

open class Viewport(left: Float = 0f,
               top: Float = 0f,
               right: Float = 0f,
               bottom: Float = 0f) : Rectangle(left, top, right, bottom) {
    //主图表区范围

    constructor(other: Viewport):this(other.left,other.top,other.right,other.bottom)

    //主图表区背景色,即画X轴与Y轴围成的区域
     val backgroundPaint: Paint by lazy {
        Paint().apply {
            style = Paint.Style.FILL
            color = Color.WHITE
        }
    }

     //是否画背景色
     var isBackgroundColorVisible = false


     //是否应用渲染模式
     var isApplayGradient:Boolean = false

     var beginColor = Color.WHITE
     var endColor = Color.WHITE
    
     var tileMode = Shader.TileMode.MIRROR
     var gridDirection = GridDirection.VERTICAL

    fun setBackgroundColor(b: Boolean, colorPlotArea: Int) {
        isBackgroundColorVisible=b
        backgroundPaint.color=colorPlotArea
    }

    fun render(canvas: Canvas) {
        drawAreaBackground(canvas)
    }

    /**
     * 绘制背景
     */
    protected fun drawAreaBackground(canvas: Canvas) {
        if (isBackgroundColorVisible) {
            if (isApplayGradient) {
                backgroundPaint.shader = if (gridDirection == GridDirection.VERTICAL) {
                    LinearGradient(
                        0f, 0f, 0f, height,
                        beginColor,endColor,
                        tileMode
                    )
                } else {
                    LinearGradient(
                        left, bottom, right, top,
                        beginColor,endColor,
                        tileMode
                    )
                }
            } else {
                backgroundPaint.shader = null
            }
            canvas.drawRect(left, top, right, bottom, backgroundPaint)
        }
    }
}